<?php

namespace App\Http\Controllers;


use Cache;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\User;
use Illuminate\Support\Facades\Hash;
use Config;
use DB;

class EditUserInfoController extends Controller
{

    public function __construct()
    {
        session_start();
    }

    public function getCustomerInfo()
    {
//        $sql = "select * from ht_customer where openid='o4ixqxCljGuSjtCTi1jUJsGFOQc4'";
        $sql = "select * from ht_customer where openid='" . $_SESSION['user']['openid'] . "'";
        $data = DB::select($sql);

        return response()->json(['errcode' => 0, 'data' => $data[0]]);
    }

    public function saveTelephone(Request $request)
    {
        $requestData = $request->only('telephone');
        $telephone = $requestData['telephone'];
        $sql = "update ht_customer set telephone='$telephone',update_time=now() where openid='" . $_SESSION['user']['openid'] . "'";
        DB::update($sql);
    }

    public function saveName(Request $request)
    {
        $requestData = $request->only('name');
        $name = $requestData['name'];
        $sql = "update ht_customer set name='$name',update_time=now() where openid='" . $_SESSION['user']['openid'] . "'";
        DB::update($sql);
    }

    public function saveEmail(Request $request)
    {
        $requestData = $request->only('email');
        $email = $requestData['email'];
        $sql = "update ht_customer set email='$email',update_time=now() where openid='" . $_SESSION['user']['openid'] . "'";
        DB::update($sql);
    }

    public function addAddress(Request $request)
    {
        $requestData = $request->only('customer_id', 'address', 'tel_telephone', 'tel_name');
        $customer_id = $requestData['customer_id'];
        $address = $requestData['address'];
        $tel_telephone = $requestData['tel_telephone'];
        $tel_name = $requestData['tel_name'];
        $sql = "insert into ht_address (customer_id,address,tel_telephone,tel_name,create_time) VALUES ($customer_id,'$address','$tel_telephone','$tel_name',now())";
        DB::insert($sql);
    }

    public function delAddress(Request $request)
    {
        $requestData = $request->only('address_id');
        $address_id = $requestData['address_id'];
        $sql = "delete from ht_address where id=$address_id";
        DB::delete($sql);
    }

    public function updateAddress(Request $request)
    {
        $requestData = $request->only('address_id', 'address', 'tel_telephone', 'tel_name');
        $address_id = $requestData['address_id'];
        $address = $requestData['address'];
        $tel_telephone = $requestData['tel_telephone'];
        $tel_name = $requestData['tel_name'];
        $sql = "update ht_address set address='$address',tel_telephone='$tel_telephone',tel_name='$tel_name',update_time=now() where id=$address_id";
        DB::update($sql);
    }

    public function getAddress(Request $request)
    {
        $requestData = $request->only('customer_id');
        $customer_id = $requestData['customer_id'];

        $sql = "select * from ht_address where customer_id=$customer_id";
        $data = DB::select($sql);
        $sql1 = "select a.address as address from ht_address as a INNER JOIN ht_customer as b ON b.use_address_id=a.id where b.id=$customer_id";
        $data1 = DB::select($sql1);
        if (count($data1) == 0) {
            if (count($data) > 0) {
                $sql3 = "insert into ht_customer(use_address_id) VALUES (" . $data[0]->id . ")";
                DB::insert($sql3);
                return response()->json(['errcode' => 0, 'data' => $data, 'active' => $data1[0]]);
            } else {

            }
        } else {
            return response()->json(['errcode' => 0, 'data' => $data, 'active' => $data1[0]]);
        }


    }

    public function setDefaultAddress(Request $request)
    {
        $requestData = $request->only('address_id', 'customer_id');
        $address_id = $requestData['address_id'];
        $customer_id = $requestData['customer_id'];
        $sql = "update ht_customer set use_address_id=$address_id where id=$customer_id";
        DB::update($sql);
    }
}
